package com.yuma.cas;

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicDemo {

    private static final long count = 0;

    public static void main(String[] args) {

        //乐观锁原理：比较和交换
        AtomicInteger i = new AtomicInteger(1);

        //比较当前值initialValue 1 和预期值expectedValue 1，相等，则将当前值换为新值
        System.out.println("第一次更新：" + i.compareAndSet(1, 200));
        System.out.println("第一次更新后i的值：" + i.get());  // 200

        System.out.println("第二次更新：" + i.compareAndSet(1, 300));
        System.out.println("第二次更新后i的值：" + i.get());

        System.out.println("第三次更新：" + i.compareAndSet(200, 300));
        System.out.println("第三次更新后i的值：" + i.get());

    }
}
